package com.hdy.framework.struts2.json;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.struts2.ServletActionContext;

import com.hdy.framework.struts2.action.BaseAction;
import com.opensymphony.xwork2.ActionInvocation;

/*自定义处理ajax请求
	by 何栋宇
	2012-11-14
 */
public class JsonResult extends BaseResult{
	private static final long serialVersionUID = -7225184616906898477L;
	private String encoding="utf8";
	private boolean isAjax=true;
	@Override
	protected void doExecute(String finalLocation, ActionInvocation invocation)
			throws Exception {
//		ServletContext sc = ServletActionContext.getServletContext();
//		HttpServletRequest request = ServletActionContext.getRequest();
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setCharacterEncoding(encoding);
		response.setContentType("application/json");
		
		PrintWriter out = response.getWriter();
		BaseAction action = (BaseAction)invocation.getAction();
		JSONObject js = new JSONObject();//JSON转换为对象
		js.put("value", action.getAjaxData());//data.value就可取的此对象
		js.put("isAjax", isAjax);
		js.write(out);
	}

}
